package com.sdhs.paas.rc.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.rc.bean.DocDiskBean;
import com.sdhs.paas.rc.bean.DocFolderBean;
import com.sdhs.paas.rc.dto.DocFolderDTO;
import com.sdhs.paas.rc.dto.TreeDTO;
import com.sdhs.paas.rc.dto.req.DocFolderReqDTO;
import com.sdhs.paas.rc.dto.req.TreeReqDTO;
import com.sdhs.paas.rc.dto.res.DocFolderResDTO;
import com.sdhs.paas.rc.dto.res.TreeResDTO;
import com.sdhs.paas.rc.service.DocDiskService;
import com.sdhs.paas.rc.service.DocFolderService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.common.entity.PaginationEntity;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

/**
 * @author DocFolder
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-12-06
 */
 
@RefreshableRestController
@RequestMapping(value = "/docFolder")
public class DocFolderController {

    @Autowired
    private DocFolderService docFolderService;
    
    @Autowired
    private DocDiskService docDiskService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getDocFolderPageList")
	public PaasBaseResponse<DocFolderResDTO> getDocFolderPageList(@RequestBody PaasBaseRequest<DocFolderReqDTO> docFolderReqDTO) {
    	DocFolderDTO docFolderDTO = docFolderReqDTO.getBody().getDocFolderDTO();
    	PageInfoDTO pageInfoDTO = docFolderReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		
		DocFolderBean docFolderBean = EntityDtoConvertUtil.convertToEntity(docFolderDTO, DocFolderBean.class);
		PaginationEntity<DocFolderBean> page = docFolderService.getPageDocFolderList(pageRequest, docFolderBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	DocFolderResDTO docFolderResDTO = new DocFolderResDTO();
		resPageInfoDTO.setRowCount(page.getTotal());
		docFolderResDTO.setPageInfo(resPageInfoDTO);
		List<DocFolderBean> docFolderBeanList = page.getRows();
		List<DocFolderDTO> dtoList = new ArrayList<DocFolderDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(docFolderBeanList, new TypeReference<List<DocFolderDTO>>() {
		});
		docFolderResDTO.setDocFolderDTOList(dtoList);
    	return new PaasBaseResponse<>(docFolderResDTO);
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveDocFolder")
	public PaasBaseResponse<DocFolderResDTO> saveDocFolder(@RequestBody PaasBaseRequest<DocFolderReqDTO> docFolderReqDTO) {
		DocFolderDTO docFolderDTO = docFolderReqDTO.getBody().getDocFolderDTO();
		DocFolderBean docFolderBean = EntityDtoConvertUtil.convertToEntity(docFolderDTO, DocFolderBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		docFolderBean.setId(uuid);
		docFolderBean.setIsDeleted(0);
		Boolean flag = docFolderService.saveDocFolder(docFolderBean);
		docFolderDTO.setId(uuid);
		DocFolderResDTO docFolderResDTO = new DocFolderResDTO();
		docFolderResDTO.setDocFolderDTO(docFolderDTO);
		return new PaasBaseResponse<>(docFolderResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateDocFolder")
	public PaasBaseResponse<DocFolderResDTO> updateDocFolder(@RequestBody PaasBaseRequest<DocFolderReqDTO> docFolderReqDTO) {
		DocFolderDTO docFolderDTO = docFolderReqDTO.getBody().getDocFolderDTO();
		DocFolderBean docFolderBean = EntityDtoConvertUtil.convertToEntity(docFolderDTO, DocFolderBean.class);
		Boolean flag = docFolderService.updateDocFolder(docFolderBean);
		DocFolderResDTO docFolderResDTO = new DocFolderResDTO();
		docFolderResDTO.setDocFolderDTO(docFolderDTO);
		return new PaasBaseResponse<>(docFolderResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getDocFolderDetail")
	public PaasBaseResponse<DocFolderResDTO> getDocFolderDetail(@RequestBody PaasBaseRequest<DocFolderReqDTO> docFolderReqDTO) {
		DocFolderDTO docFolderDTO = docFolderReqDTO.getBody().getDocFolderDTO();
		DocFolderBean docFolderBean = EntityDtoConvertUtil.convertToEntity(docFolderDTO, DocFolderBean.class);
		docFolderBean = docFolderService.getDocFolderDetail(docFolderBean.getId());
		DocFolderResDTO docFolderResDTO = new DocFolderResDTO();
		docFolderResDTO.setDocFolderDTO(docFolderDTO);
		return new PaasBaseResponse<>(docFolderResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteDocFolder")
	public PaasBaseResponse<DocFolderResDTO> deleteDocFolder(@RequestBody PaasBaseRequest<DocFolderReqDTO> docFolderReqDTO) {
		DocFolderDTO docFolderDTO = docFolderReqDTO.getBody().getDocFolderDTO();
		DocFolderBean docFolderBean = EntityDtoConvertUtil.convertToEntity(docFolderDTO, DocFolderBean.class);
		Boolean flag = docFolderService.deleteDocFolder(docFolderBean);
		DocFolderResDTO docFolderResDTO = new DocFolderResDTO();
		docFolderResDTO.setDocFolderDTO(docFolderDTO);
		return new PaasBaseResponse<>(docFolderResDTO);
	}
	
	/**
	 * 关联菜单
	 * */
	@PostMapping("/getDiskFolederTreeData")
	public PaasBaseResponse<TreeResDTO> getTreeData(@RequestBody PaasBaseRequest<TreeReqDTO> treeReqDTO) {
		//获取请求参数
		TreeDTO treeDTO = treeReqDTO.getBody().getTreeDTO();
//		String treeId = treeDTO.getId();
//		String name = treeDTO.getLabel();
		//查询所有的网盘
		DocDiskBean param = new DocDiskBean();
		List<DocDiskBean> docDiskBeanList = docDiskService.getList(param);
		//查询所有的文件夹
		DocFolderBean docFolderBean = null;
		List<TreeDTO> treeDTOList = new ArrayList<TreeDTO>();
		for (DocDiskBean docDiskBean : docDiskBeanList) {
			treeDTO = new TreeDTO();
			treeDTO.setId(docDiskBean.getId());
			treeDTO.setIsLeaf(0);
			treeDTO.setLabel(docDiskBean.getUserName()+"-"+docDiskBean.getDiskName());
			treeDTO.setType("disk");
			treeDTO.setParentId("root");
			treeDTO.setChildren(this.getTreeDTOList(treeDTO));
			treeDTOList.add(treeDTO);
		}
//		//循环填充树的节点内容,第一级节点是网盘列表，根节点是root，网盘的type为disk，文件夹的type为folder
		TreeResDTO treeResDTO =new TreeResDTO();
		treeResDTO.setTreeDTOList(treeDTOList);
		return new PaasBaseResponse<>(treeResDTO);
	}
	
	private List<TreeDTO> getTreeDTOList(TreeDTO tree){
		List<TreeDTO> treeDTOList = new ArrayList<TreeDTO>();
		DocFolderBean docFolderBean = new DocFolderBean();
		String parentId = tree.getId();
		docFolderBean.setPid(parentId);
		List<DocFolderBean> docFolderBeanList = docFolderService.getList(docFolderBean);
		TreeDTO treeDTO = null;
		for (DocFolderBean folder : docFolderBeanList) {
			treeDTO = new TreeDTO();
			treeDTO.setId(folder.getId());
			treeDTO.setLabel(folder.getName());
			treeDTO.setParentId(parentId);
			treeDTO.setChildren(this.getTreeDTOList(treeDTO));
			treeDTO.setType("folder");
			treeDTOList.add(treeDTO);
		}
		return treeDTOList;
	}
}
